package com.youlu.campus.web.course;

import com.alibaba.fastjson.JSON;
import com.youlu.campus.entity.course.dto.ActivityPopupQueryDTO;
import com.youlu.campus.entity.course.dto.CourseInfoDTO;
import com.youlu.campus.entity.course.dto.HotActivityDTO;
import com.youlu.campus.entity.course.dto.KosActivityDTO;
import com.youlu.campus.entity.course.dto.RecommendActivityDTO;
import com.youlu.campus.entity.course.group.Info;
import com.youlu.campus.entity.vote.dto.OrgInfoSearchDTO;
import com.youlu.campus.entity.vote.dto.VoteHomeSearchDTO;
import com.youlu.campus.service.card.TsCardService;
import com.youlu.campus.service.course.ActivityPopupService;
import com.youlu.campus.service.course.LiteHomeService;
import com.youlu.campus.service.course.UserCourseOrderService;
import com.youlu.campus.web.vote.service.ActivityVoteHomeService;
import com.yuelin.infrastructure.quantum.common.QResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @author : [dengbo]
 * @className : WechatTest
 * @description : [描述说明该类的功能]
 * @createTime : [2022/5/24 21:18]
 */
@Slf4j
@RestController
//@RequestMapping("/public")
public class LiteHomePublicController {
    @Value("${sys.pay.wx.payWechatNotifyUrl}")
    private String notifyUrl;

    @Autowired
    private UserCourseOrderService userCourseOrderService;

    @Resource
    private LiteHomeService liteHomeService;

    @Autowired
    private ActivityVoteHomeService activityVoteHomeService;

    @Autowired
    private ActivityPopupService activityPopupService;

    @Autowired
    private TsCardService tsCardService;

    /**
     * 课程支付
     * 参数：request - 课程支付请求类
     */
    @RequestMapping(value = "/public/pay/test", method = {RequestMethod.GET, RequestMethod.POST})
    public QResult coursePay(HttpServletRequest httpRequest) {
        Object data = userCourseOrderService.payTest(notifyUrl);
        log.info("LiteHomeController,courseInfo,response:{}", data);
        return QResult.success(data);
    }

    /**
     * 课程信息
     * 参数：request - 课程信息
     */
    @RequestMapping(value = "/public/course/info", method = {RequestMethod.GET, RequestMethod.POST})
    public QResult courseInfo(@Validated(Info.class) @RequestBody CourseInfoDTO request) {
        log.info("LiteHomeController,courseInfo,request param:{}", JSON.toJSONString(request));
        return QResult.success(liteHomeService.getCourseInfo(request));
    }

    /**
     * 热门活动
     * 参数：request - 热门活动请求类
     */
    @PostMapping("/public/hot/activity/page/info")
    public QResult getHotActivityList(@Validated @RequestBody HotActivityDTO request) {
        log.info("LiteHomePublicController,getHotActivityList,request param:{}", JSON.toJSONString(request));
        return QResult.success(liteHomeService.getHotList(request));
    }

    /**
     * 推荐活动列表
     * 参数：request - 推荐活动列表
     */
    @PostMapping("/public/recommend/activity/page/info")
    public QResult getRecommendActivityList(@Validated @RequestBody RecommendActivityDTO request) {
        log.info("LiteHomePublicController,getRecommendActivityList,request param:{}", JSON.toJSONString(request));
        return QResult.success(liteHomeService.getRecommendList(request));
    }

    @PostMapping("/public/recommend/activity/page/list")
    public QResult getPageHomeActivityList(@RequestBody RecommendActivityDTO request) {
        log.info("LiteHomePublicController,getPageHomeActivityList,request param:{}", JSON.toJSONString(request));
        return QResult.success(liteHomeService.getRecommendPageHomeList(request));
    }

    /**
     * 活动详情
     * 参数：request - 活动详情
     */
    @GetMapping("/public/get/activity/detail")
    public QResult getRecruitmentActivityById(@Validated @RequestParam("activityId") String activityId) {
        log.info("LiteHomePublicController,getRecruitmentActivityById,request param:{}", activityId);
        return QResult.success(liteHomeService.getRecruitmentActivityById(activityId));
    }

    /**
     * 活动详情
     * 参数：request - 活动详情
     */
    @PostMapping("/public/get/kos/activity/detail")
    public QResult getKosActivityDetail(@Validated @RequestBody KosActivityDTO request) {
        log.info("LiteHomePublicController,getKosActivityDetail,request param:{}", request);
        return QResult.success(liteHomeService.getKosActivityDetail(request));
    }

    /**
     * 投票首页列表
     * 参数：request - 投票首页列表
     */
    @PostMapping("/public/org/vote/rank")
    public QResult getOrgVoteRankPage(@Validated @RequestBody VoteHomeSearchDTO request) {
        log.info("LiteHomePublicController,getOrgVoteRankPage,request param:{}", JSON.toJSONString(request));
        return QResult.success(activityVoteHomeService.getOrgVoteRankPage(request));
    }

    /**
     * 组织信息搜索
     * 参数：request - 投票首页信息
     */
    @PostMapping("/public/org/info/search")
    public QResult orgInfoSearch(@Validated(Info.class) @RequestBody OrgInfoSearchDTO request) {
        return QResult.success(activityVoteHomeService.getOrgPageInfo(request));
    }

    /**
     * 弹窗信息查询
     * 参数：request - 弹窗信息查询
     */
    @PostMapping("/public/query/activity/popup")
    public QResult queryPopupInfo(@Validated(Info.class) @RequestBody ActivityPopupQueryDTO request) {
        return QResult.success(activityPopupService.findActivityResult(request));
    }

    /**
     * 支付通知
     * 参数：request - 支付通知
     */
    @PostMapping("/public/card/payNotify")
    public QResult cardPayNotity(@RequestParam Map<String, String> params) {
        log.info("card notify param:{}", params);
        tsCardService.cardPayNotify(params);
        return QResult.success(true);
    }

    /**
     * 价格变更通知
     * 参数：request - 价格变更通知
     */
    @PostMapping("/card/price/update/notify")
    public QResult priceUpdateNotity(@RequestParam Map<String, String> params) {
        log.info("card price update notify param:{}", params);
        tsCardService.cardPriceUpdateNotify(params);
        return QResult.success("ok");
    }
}